package com.ruoyi.web.controller.system;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.ruoyi.common.utils.ALiYunSms;
import com.ruoyi.common.utils.BaiDuSms;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Cash;
import com.ruoyi.system.service.ICashService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分提现Controller
 * 
 * @author ruoyi
 * @date 2019-11-07
 */
@Controller
@RequestMapping("/system/cash")
public class CashController extends BaseController
{
    private String prefix = "system/cash";

    @Autowired
    private ICashService cashService;
    @Autowired
    private IMemberService memberService;

    @RequiresPermissions("system:cash:view")
    @GetMapping()
    public String cash()
    {
        return prefix + "/cash";
    }

    /**
     * 查询积分提现列表
     */
    @RequiresPermissions("system:cash:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Cash cash)
    {
        startPage();
        List<Cash> list = cashService.selectCashList(cash);
        for(Cash ca:list){
            if(ca.getMemberId()!=null && !ca.getMemberId().toString().equals("")){
                Member memeber = memberService.selectMemberById(ca.getMemberId());
                ca.setMemberName(memeber.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出积分提现列表
     */
    @RequiresPermissions("system:cash:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Cash cash)
    {
        List<Cash> list = cashService.selectCashList(cash);
        ExcelUtil<Cash> util = new ExcelUtil<Cash>(Cash.class);
        return util.exportExcel(list, "cash");
    }

    /**
     * 新增积分提现
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存积分提现
     */
    @RequiresPermissions("system:cash:add")
    @Log(title = "积分提现", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Cash cash)
    {
        return toAjax(cashService.insertCash(cash));
    }

    /**
     * 修改积分提现
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Cash cash = cashService.selectCashById(id);
        mmap.put("cash", cash);
        return prefix + "/edit";
    }

    /**
     * 修改保存积分提现
     */
    @RequiresPermissions("system:cash:edit")
    @Log(title = "积分提现", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Cash cash)
    {
        return toAjax(cashService.updateCash(cash));
    }

    /**
     * 完成积分提现
     */
    @Log(title = "完成积分提现", businessType = BusinessType.UPDATE)
    @PostMapping("/passStatus")
    @ResponseBody
    public AjaxResult passStatus(Cash cash)
    {
        Member member = memberService.selectMemberById(cash.getMemberId());
        try {
            ALiYunSms.sendSms(member.getTelephone(), member.getNickName(), "提现成功，抵消", cash.getMoney());
//             BaiDuSms.sendSms(member.getTelephone(), member.getNickName(), "提现成功，抵消", cash.getMoney());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(cashService.updateCash(cash));
    }


    /**
     * 删除积分提现
     */
    @RequiresPermissions("system:cash:remove")
    @Log(title = "积分提现", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cashService.deleteCashByIds(ids));
    }
}
